namespace Warehouse

open System
open System.Windows.Data
open FSharp.Windows

[<AbstractClass>]
[<AllowNullLiteral>]
type ClientProductCommissionModel() =
    inherit EntityModel<DataModel.ClientProductCommission>()

    let mutable product : DataModel.Product = null
    member this.Product with get() = product and set value = product <- value

    member this.Title = product.Title()
    abstract Value : decimal Nullable with get, set

    override this.Init() = 
        this.Value <- Nullable this.Entity.Value

    override this.Flush() = 
        this.Entity.Value <- this.Value.Value 
        if this.Entity.Product = null then
            this.Entity.Product <- product

[<AllowNullLiteral>]
[<AbstractClass>]
type ClientBrandCommissionModel() =
    inherit EntityModel<DataModel.ClientBrandCommission>()

    let mutable brand : DataModel.Brand = null
    member this.Brand with get() = brand and set value = brand <- value

    member this.Default = sprintf "%M,%M" brand.ClientDefaultRegularProductCommission brand.ClientDefaultLitrSizeProductCommission
    abstract RegularSizeCommission : decimal Nullable with get, set
    abstract LitrSizeCommission : decimal Nullable  with get, set
    member this.HasValues = this.RegularSizeCommission.HasValue || this.LitrSizeCommission.HasValue
    abstract ProductCommissions : ClientProductCommissionModel CollectionView with get, set

    abstract AllSizesFilter : bool Nullable with get, set
    abstract RegularSizeFilter : bool Nullable with get, set
    abstract LitrSizeFilter : bool Nullable with get, set

    override this.Init() = 
        this.RegularSizeCommission <- Nullable this.Entity.RegularProducts
        this.LitrSizeCommission <- Nullable this.Entity.LitrSizeProducts

    override this.Flush() = 
        this.Entity.RegularProducts <- this.RegularSizeCommission.Value 
        this.Entity.LitrSizeProducts <- this.LitrSizeCommission.Value 
        if this.Entity.Brand = null then
            this.Entity.Brand <- brand

    interface ComponentModel.IEditableObject with
        member this.BeginEdit() = ()
        member this.EndEdit() = 
            match this.RegularSizeCommission.AsOption(), this.LitrSizeCommission.AsOption() with
            | Some regular, None -> this.LitrSizeCommission <- this.RegularSizeCommission
            | None, Some litrSize -> this.RegularSizeCommission <- this.LitrSizeCommission
            | _ -> () 
        member this.CancelEdit() = ()

[<AbstractClass>]
type ClientDetailsModel() = 
    inherit EntityModel<DataModel.Client>()

    //Client details dialog
    abstract CompanyName : string with get, set

    abstract BillToAddress : string with get, set
    abstract BillToCity : string with get, set
    abstract BillToState : string with get, set
    abstract BillToZipCode : string with get, set

    abstract ShipToAddress : string with get, set
    abstract ShipToCity : string with get, set
    abstract ShipToState : string with get, set
    abstract ShipToZipCode : string with get, set

    abstract EMail : string with get, set
    abstract Attn : string with get, set
    abstract Phone : string with get, set
    abstract Fax : string with get, set

    //Pct view client details
    abstract Id : int with get, set
    abstract TotalSales : decimal with get, set
    abstract BrandCommissions : ClientBrandCommissionModel[] with get, set
    abstract SelectedBrand : ClientBrandCommissionModel with get, set

    override this.Init() =
        let e = this.Entity
        this.CompanyName <- e.CompanyName
        this.BillToAddress <- e.BillToAddress
        this.BillToCity <- e.BillToCity
        this.BillToState <- e.BillToState
        this.BillToZipCode <- e.BillToZipCode

        this.ShipToAddress <- e.ShipToAddress
        this.ShipToCity <- e.ShipToCity
        this.ShipToState <- e.ShipToState
        this.ShipToZipCode <- e.ShipToZipCode

        this.EMail <- e.EMail
        this.Attn <- e.Attn
        this.Phone <- e.Phone

        this.Id <- e.Id

    override this.Flush() =
        let e = this.Entity

        e.CompanyName <- this.CompanyName

        e.BillToAddress <- this.BillToAddress
        e.BillToCity <- this.BillToCity
        e.BillToState <- this.BillToState
        e.BillToZipCode <- this.BillToZipCode

        e.ShipToAddress <- this.ShipToAddress
        e.ShipToCity <- this.ShipToCity
        e.ShipToState <- this.ShipToState
        e.ShipToZipCode <- this.ShipToZipCode

    